<template>
  <el-drawer v-model="isVisible" title="样式编辑" :with-header="true" direction="rtl" size="400px">
    <div class="style-editor">
      <div class="style-options">
        <el-form :model="style" label-width="120px">
          <!-- 文字样式编辑区域 -->
          <template v-if="style.fontFamily">
            <el-form-item label="字体大小">
              <el-input-number v-model="style.fontSize" :min="8" :max="72" />
            </el-form-item>
            <el-form-item label="字体">
              <el-select v-model="style.fontFamily">
                <el-option label="微软雅黑" value="Microsoft YaHei" />
                <el-option label="宋体" value="SimSun" />
                <el-option label="黑体" value="SimHei" />
                <el-option label="Arial" value="Arial" />
              </el-select>
            </el-form-item>
            <el-form-item label="字体颜色">
              <el-color-picker v-model="style.color" />
            </el-form-item>
            <el-form-item label="字体伸缩">
              <el-select v-model="style.fontStretch">
                <el-option label="正常" value="normal" />
                <el-option label="收缩" value="condensed" />
                <el-option label="展开" value="expanded" />
              </el-select>
            </el-form-item>
            <el-form-item label="行高">
              <el-input-number v-model="style.lineHeight" :min="0.5" :max="100" :step="0.1" />
            </el-form-item>
            <el-form-item label="对齐方式">
              <el-select v-model="style.textAlign">
                <el-option label="左对齐" value="left" />
                <el-option label="居中" value="center" />
                <el-option label="右对齐" value="right" />
              </el-select>
            </el-form-item>
            <el-form-item label="字体样式">
              <el-select v-model="style.fontStyle">
                <el-option label="正常" value="normal" />
                <el-option label="斜体" value="italic" />
                <el-option label="倾斜" value="oblique" />
              </el-select>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label="填充颜色">
              <el-color-picker v-model="style.fill" />
            </el-form-item>
            <el-form-item label="边框颜色">
              <el-color-picker v-model="style.stroke" />
            </el-form-item>
            <el-form-item label="边框宽度">
              <el-input-number v-model="style.width" :min="0" />
            </el-form-item>
            <el-form-item label="透明度">
              <el-slider v-model="style.opacity" :min="0" :max="1" :step="0.1" />
            </el-form-item>
          </template>
        </el-form>
      </div>
      <div class="button-group">
        <el-button type="primary" @click="confirmStyle">确认</el-button>
        <el-button type="danger" @click="confirmDelete">删除</el-button>
      </div>
    </div>
    <el-dialog title="确认删除" v-model="deleteDialogVisible" width="30%" align-center>
      <span>您确定要删除该要素吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="danger" @click="deleteFeature">确定</el-button>
      </span>
    </el-dialog>
  </el-drawer>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  feature: {
    type: Object,
    default: null
  },
  feaStyle: {
    type: Object,
    default: null
  },
})

const emit = defineEmits(['update:visible', 'confirm', 'delete'])

const isVisible = ref(props.visible)
const style = ref({
  fill: props.feaStyle?.getFill().getColor() || '#409EFF',
  stroke: props.feaStyle?.getStroke().getColor() || '#000000',
  width: props.feaStyle?.getStroke().getWidth() || 1,
  opacity: props.feaStyle?.getFill().getColor().split(',')[3].replace(')', '') || 1,
  // 文字样式属性
  fontSize: (() => {
    if (!props.feaStyle?.fontSize) return 14;
    const matches = props.feaStyle.fontSize.toString().match(/\d+/);
    return matches ? parseInt(matches[0]) : 14;
  })(),
  fontFamily: props.feaStyle?.fontFamily || '',
  color: props.feaStyle?.color || '#000000',
  fontStretch: props.feaStyle?.fontStretch || 'normal',
  lineHeight: props.feaStyle?.lineHeight || 1,
  textAlign: props.feaStyle?.textAlign || 'left',
  fontStyle: props.feaStyle?.fontStyle || 'normal' })
const deleteDialogVisible = ref(false)

watch(() => props.visible, (val) => {
  isVisible.value = val
})

watch(() => isVisible.value, (val) => {
  emit('update:visible', val)
})

watch(() => props.feature, (val) => {
  if (val) {
    const featureStyle = props.feaStyle.value
    if (featureStyle) {
      if (featureStyle.fontFamily) {
        style.value = {
          fontSize: featureStyle.fontSize,
          fontFamily: featureStyle.fontFamily,
          color: featureStyle.color,
          fontWeight: featureStyle.fontWeight,
          fontStretch: featureStyle.fontStretch,
          lineHeight: featureStyle.lineHeight,
          textAlign: featureStyle.textAlign,
          fontStyle: featureStyle.fontStyle,
          fontVariant: featureStyle.fontVariant
        }
      } else {
        style.value = {
          fill: featureStyle.getFill().getColor(),
          stroke: featureStyle.getStroke().getColor(),
          width: featureStyle.getStroke().getWidth(),
          opacity: featureStyle.getFill().getColor().split(',')[3].replace(')', '')
        }
      }
    }
  }
})
watch(() => props.feaStyle, (val) => {
  if (val) {
    const featureStyle = val
    if (featureStyle) {
      if (featureStyle.fontFamily) {
        style.value = {
          fontSize: featureStyle.fontSize,
          fontFamily: featureStyle.fontFamily,
          color: featureStyle.color,
          fontWeight: featureStyle.fontWeight,
          fontStretch: featureStyle.fontStretch,
          lineHeight: featureStyle.lineHeight,
          textAlign: featureStyle.textAlign,
          fontStyle: featureStyle.fontStyle,
          fontVariant: featureStyle.fontVariant
        }
      } else {
        style.value = {
          fill: featureStyle.getFill().getColor(),
          stroke: featureStyle.getStroke().getColor(),
          width: featureStyle.getStroke().getWidth(),
          opacity: featureStyle.getFill().getColor().split(',')[3].replace(')', '')
        }
      }
    }
  }
})

const confirmStyle = () => {
  emit('confirm', style.value)
  isVisible.value = false
}

const confirmDelete = () => {
  deleteDialogVisible.value = true
}

const deleteFeature = () => {
  emit('delete')
  deleteDialogVisible.value = false
  isVisible.value = false
}
</script>

<style scoped>
.style-editor {
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

h3 {
  margin-bottom: 20px;
  font-size: 20px;
  color: #333;
  text-align: center;
}

.style-options {
  margin-bottom: 20px;
  flex: 1;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>